//
//  WmcTeamMemberQRCodeInviteVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/14.
//

import UIKit

// MARK: - 扩展WmcTeamMemberQRCodeInviteVC + WmcNavigatorProtocol

extension WmcTeamMemberQRCodeInviteVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.teamInviteQRCodePage]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vc = WmcTeamMemberQRCodeInviteVC()
        if let teamAblum = parms.context as? WmcMediaTeamAlbumModel {
            let vm = WmcTeamMemberQRCodeInviteVM()
            vm.teamAblum = teamAblum
            vc = WmcTeamMemberQRCodeInviteVC(vm)
        }
        return vc
    }
}

// MARK: - 扩展WmcTeamMemberQRCodeInviteVC + WmcVMProtocol

extension WmcTeamMemberQRCodeInviteVC: WmcVMProtocol {
    typealias VMType = WmcTeamMemberQRCodeInviteVM
}

/// 二维码邀请
class WmcTeamMemberQRCodeInviteVC: WmcBaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()

        // 获取邀请相关的信息
        viewModel.getQrcode()
    }

    override func makeUI() {
        makeNavView(showShadow: false)
        title = "二维码邀请"
        view.backgroundColor = UIColor.wmc_gray_F7F8FA

        view.addSubview(qrCodeView)
        view.addSubview(downloadBtn)
        view.addSubview(wechatShareButton)

        qrCodeView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        let btnW = Wmc_screenWidth * 166 / 375.0
        let btnH = btnW * 48 / 166.0

        downloadBtn.snp.makeConstraints { make in
            make.top.equalTo(qrCodeView.snp.bottom).offset(36)
            make.left.equalTo(16)
            make.width.equalTo(btnW)
            make.height.equalTo(btnH)
        }

        wechatShareButton.snp.makeConstraints { make in
            make.top.equalTo(downloadBtn.snp.top)
            make.left.equalTo(downloadBtn.snp.right).offset(11)
            make.width.equalTo(btnW)
            make.height.equalTo(btnH)
        }
    }

    override func bindViewModel() {
        qrCodeView.teamIdLabel.text = "团队号:\(viewModel.teamAblum?.teamOpenId ?? "")"

        viewModel.inviationInfo.bind { [weak self] info in
            guard let weakSelf = self, let info = info, let image = info.url.yp.qrCodeImage else { return }
            weakSelf.qrCodeView.qrCodeImgView.image = image
            weakSelf.qrCodeView.appIconImgView.image = UIImage.YP.AppIcon
        }.disposed(by: rx.disposeBag)
    }

    override func bindViewAction() {
        downloadBtn.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.saveImgToLocalAlbum(img: weakSelf.qrCodeView.yp.screenshot())
        }.disposed(by: rx.disposeBag)

        wechatShareButton.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self, let _ = weakSelf.viewModel.inviationInfo.value else { return }
            weakSelf.viewModel.shareQrcodeToWechat(img: weakSelf.qrCodeView.yp.screenshot())
        }.disposed(by: rx.disposeBag)
    }

    private func showShareView(_ shareAlertView: WmcShareAlertView) {
        let height = Wmc_safeAreaBottom + 214
        let presentVC = WmcPresentVC(config: .init(style: .sheet(), contentView: shareAlertView, contentLayoutBlock: { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(height)
        }))
        presentVC.show()
    }

    lazy var qrCodeView: WmcTeamQRCodeView = {
        let view = WmcTeamQRCodeView()
        return view
    }()

    lazy var downloadBtn: WmcLeftImgRightTextBtn = {
        let view = WmcLeftImgRightTextBtn(title: "保存二维码",
                                          image: "download_qrCode_icon".wmc_image)
        view.backgroundColor = UIColor.wmc_yellow_FF9305
        view.contentEdge = .YP.Create(horizontal: 23, vertical: 11)
        view.font = 18.wmc_font
        view.imgSize = 22.wmc_autoHor
        view.textColor = UIColor.wmc_white_FFFFFF
        view.imgTextSpacing = 2
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()

    lazy var wechatShareButton: WmcLeftImgRightTextBtn = {
        let view = WmcLeftImgRightTextBtn(title: "分享到微信",
                                          image: "wechat_share_icon".wmc_image)
        view.backgroundColor = UIColor.wmc_green_04BB04
        view.contentEdge = .YP.Create(horizontal: 23, vertical: 11)
        view.font = 18.wmc_font
        view.imgSize = 22.wmc_autoHor
        view.textColor = UIColor.wmc_white_FFFFFF
        view.imgTextSpacing = 2
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()
}
